﻿using DGFramework;
using ETModel;
using NPOI.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Threading.Tasks;
using DG.Tweening;
using NUnit.Framework.Internal.Execution;

namespace ETHotfix
{

    public class UIMoreOptionsComponent : Component
    {
        private UIGamePageComponent UigamePage;


        #region//更多选项

        #region//更多选项进入页面
        public GameObject Obj_MoreOptions;
        private GameObject Obj_MoreButton;


        private Button Btn_ExitMoreOptions;
        private Button Btn_MoreBoundFB;

        private Image Img_MoreHisHeadIs;

        private Text Txt_MoreMyname;
        #endregion

        #region//排行榜
        private GameObject Obj_RankingList;
        private GameObject Obj_ScrollView;

        private Button Btn_FriendsList;
        private Button Btn_WorldRankings;
        private Button Btn_LocalRankings;
        private Button Btn_ExitTheRankings;

        #endregion

        #region//邀请召回
        private GameObject Obj_InviteFriend;
        private Button Btn_ExitInviteFriend;
        private Button Btn_InviteButton;
        private Button Btn_RecallFriends;


        #endregion

        #region//VIP
        private GameObject Obj_VipTc;
        private Button Btn_ExitViptc;

        private Button Btn_VIpMonth;
        private Button Btn_VipWeek;
        #endregion

        #region//地图

        #endregion

        #region//消息
        private GameObject Obj_GameNewsBack;
        private Button Btn_ExitGameNewsBack;
        private GameObject Obj_Scrollhuadong;
        #endregion


        #region//设置
        private GameObject Obj_GameSettingBack;

        private Button Btn_SetUpTheSound;
        private Button Btn_SetUpTheMusic;
        private Button Btn_SetTheVibration;
        private Button Btn_SetVIP;
        private Button Btn_SetUpNotifications;
        private Button Btn_OpenCustomerService;
        private Button Btn_ExitGameSeting;

        #endregion


        #region//好友
        private GameObject Obj_FriendListBacK;
        private Button Btn_ExitFriendList;
        private Button Btn_InviteFBFriends;
        #endregion

        #region//修改个人信息
        public GameObject Obj_AmendinfoBack;
        private Button Btn_ExitAmendinfo;
        #endregion

        private GameObject Fuchou_Panel;
        private Button Exit_fuchoubang;
        #endregion

        public ReferenceCollector MoreOptionsResources;
        private UICustomsComponent Uicustmos;
        public void Awake() 
        {

            UI ui = this.GetParent<UI>();
            UigamePage = ui.GetComponent<UIGamePageComponent>();
            Log.Debug("name-----------------===" + UigamePage.GameObject);
            MoreOptionsResources = this.GetParent<UI>().GameObject.transform.Find("MoreOptionsResources").GetComponent<ReferenceCollector>();
            #region//更多选项

            #region//更多选项进入页面
            Obj_MoreOptions = MoreOptionsResources.Get<GameObject>("Obj_MoreOptions");
            Obj_MoreButton = MoreOptionsResources.Get<GameObject>("Obj_MoreButton");
            for (int TheCurrentDigital = 0; TheCurrentDigital < 8; TheCurrentDigital++)
            {
                int prnumber = TheCurrentDigital;
                Obj_MoreButton.transform.GetChild(TheCurrentDigital).GetComponent<Button>().onClick.Add(() => { OnRunyemian(prnumber); });

            }
            Btn_ExitMoreOptions = MoreOptionsResources.Get<GameObject>("Btn_ExitMoreOptions").GetComponent<Button>();
            Btn_ExitMoreOptions.onClick.Add(OnBtnExitMore);
            Btn_MoreBoundFB = MoreOptionsResources.Get<GameObject>("Btn_MoreBoundFB").GetComponent<Button>();
            Btn_MoreBoundFB.onClick.Add(OnBtnMorebangding);

            Txt_MoreMyname = MoreOptionsResources.Get<GameObject>("Txt_MoreMyname").GetComponent<Text>();

            Img_MoreHisHeadIs = MoreOptionsResources.Get<GameObject>("Img_MoreHisHeadIs").GetComponent<Image>();

            Img_MoreHisHeadIs.GetComponent<Button>().onClick.Add(AmendInfo);

            Fuchou_Panel = MoreOptionsResources.Get<GameObject>("Fuchou_Panel");
            Exit_fuchoubang = MoreOptionsResources.Get<GameObject>("Exit_fuchoubang").GetComponent<Button>();
            Exit_fuchoubang.onClick.Add(OnExitfuchoubang);
            #endregion


            #region//排行榜
            Obj_RankingList = MoreOptionsResources.Get<GameObject>("Obj_RankingList");
            Btn_FriendsList = MoreOptionsResources.Get<GameObject>("Btn_FriendsList").GetComponent<Button>();
            Btn_FriendsList.onClick.Add(OnBtnhaoyouRanking);
            Btn_WorldRankings = MoreOptionsResources.Get<GameObject>("Btn_WorldRankings").GetComponent<Button>();
            Btn_WorldRankings.onClick.Add(OnBtn_hijieRanking);
            Btn_LocalRankings = MoreOptionsResources.Get<GameObject>("Btn_LocalRankings").GetComponent<Button>();
            Btn_LocalRankings.onClick.Add(OnBtnbendiRanking);
            Btn_ExitTheRankings = MoreOptionsResources.Get<GameObject>("Btn_ExitTheRankings").GetComponent<Button>();
            Btn_ExitTheRankings.onClick.Add(OnBtnExitRanking);
            Obj_ScrollView = MoreOptionsResources.Get<GameObject>("Obj_ScrollView");
            #endregion


            #region//邀请召回
            Obj_InviteFriend = MoreOptionsResources.Get<GameObject>("Obj_InviteFriend");

            Btn_ExitInviteFriend = MoreOptionsResources.Get<GameObject>("Btn_ExitInviteFriend").GetComponent<Button>();
            Btn_ExitInviteFriend.onClick.Add(OnBtnExitInvitationRecall);
            Btn_InviteButton = MoreOptionsResources.Get<GameObject>("Btn_InviteButton").GetComponent<Button>();
            Btn_InviteButton.onClick.Add(OnBtnInvitation);
            Btn_RecallFriends = MoreOptionsResources.Get<GameObject>("Btn_RecallFriends").GetComponent<Button>();
            Btn_RecallFriends.onClick.Add(OnBtnrecall);

            #endregion

            #region//VIP
            Obj_VipTc = MoreOptionsResources.Get<GameObject>("Obj_VipTc");
            Btn_ExitViptc = MoreOptionsResources.Get<GameObject>("Btn_ExitViptc").GetComponent<Button>();
            Btn_ExitViptc.onClick.Add(OnBtnExitViptc);

            Btn_VIpMonth = MoreOptionsResources.Get<GameObject>("Btn_VIpMonth").GetComponent<Button>();
            Btn_VIpMonth.onClick.Add(OnBtnVIpMonth);

            Btn_VipWeek = MoreOptionsResources.Get<GameObject>("Btn_VipWeek").GetComponent<Button>();
            Btn_VipWeek.onClick.Add(OnBtnVipWeek);

            #endregion

            #region//地图


            #endregion

            #region//消息
            Obj_GameNewsBack = MoreOptionsResources.Get<GameObject>("Obj_GameNewsBack");
            Btn_ExitGameNewsBack = MoreOptionsResources.Get<GameObject>("Btn_ExitGameNewsBack").GetComponent<Button>();
            Btn_ExitGameNewsBack.onClick.Add(OnBtnExitMessage);
            Obj_Scrollhuadong = MoreOptionsResources.Get<GameObject>("Obj_Scrollhuadong");
            #endregion


            #region//设置
            Obj_GameSettingBack = MoreOptionsResources.Get<GameObject>("Obj_GameSettingBack");

            Btn_SetUpTheSound = MoreOptionsResources.Get<GameObject>("Btn_SetUpTheSound").GetComponent<Button>();
            Btn_SetUpTheSound.onClick.Add(OnBtnsetyinxiao);

            Btn_SetUpTheMusic = MoreOptionsResources.Get<GameObject>("Btn_SetUpTheMusic").GetComponent<Button>();
            Btn_SetUpTheMusic.onClick.Add(OnBtnsetyinyue);

            Btn_SetTheVibration = MoreOptionsResources.Get<GameObject>("Btn_SetTheVibration").GetComponent<Button>();
            Btn_SetTheVibration.onClick.Add(OnBtnsetzhengdong);


            Btn_ExitGameSeting = MoreOptionsResources.Get<GameObject>("Btn_ExitGameSeting").GetComponent<Button>();
            Btn_ExitGameSeting.onClick.Add(OnBtnExiteSeting);

            #endregion


            #region//好友
            Obj_FriendListBacK = MoreOptionsResources.Get<GameObject>("Obj_FriendListBacK");

            Btn_ExitFriendList = MoreOptionsResources.Get<GameObject>("Btn_ExitFriendList").GetComponent<Button>();
            Btn_ExitFriendList.onClick.Add(OnBtnExitFriendl);
            Btn_InviteFBFriends = MoreOptionsResources.Get<GameObject>("Btn_InviteFBFriends").GetComponent<Button>();
            Btn_InviteFBFriends.onClick.Add(OnBtnInviteFriends);
            #endregion

            #region//修改个人信息
            Obj_AmendinfoBack = MoreOptionsResources.Get<GameObject>("Obj_AmendinfoBack");
            Btn_ExitAmendinfo = MoreOptionsResources.Get<GameObject>("Btn_ExitAmendinfo").GetComponent<Button>();
            Btn_ExitAmendinfo.onClick.Add(OnBtnExitAmendinfo);

            #endregion

            Txt_MoreMyname.text = UigamePage.Myname;
            Img_MoreHisHeadIs.sprite = UigamePage.MySprite;
            #endregion
        }



        #region//更多选项

        #region//更多选项进入页面
        private void OnBtnMorebangding()
        {
            Log.Debug("绑定facebook");
        }

        private void AmendInfo()
        {
            Obj_AmendinfoBack.SetActive(true);
        }

        private void OnBtnExitMore()
        {
            Obj_MoreOptions.SetActive(false);
        }

        private void OnRunyemian(int numbers)
        {
            Obj_MoreOptions.SetActive(false);
            switch (numbers)
            {
                case 0://排行榜
                    GetRanklist().Coroutine();
                    Obj_RankingList.SetActive(true);
                    break;
                case 1://排邀请召回
                    Obj_InviteFriend.SetActive(true);
                    break;
                case 2://vip
                    Obj_VipTc.SetActive(true);
                    break;
                case 3://地图
                    if (Uicustmos == null)
                    {
                        UI ui = this.GetParent<UI>();
                        Uicustmos = ui.GetComponent<UICustomsComponent>();
                        Uicustmos.OnHuoqu();

                    }
                    UigamePage.Obj_TheMapPage.SetActive(true);
                    UigamePage.Obj_TheMapPage.transform.localScale = new Vector3(1, 1, 1);
                    break;
                case 4://消息
                    Obj_GameNewsBack.SetActive(true);
                    Obj_GameNewsBack.transform.localScale = new Vector3(1, 1, 1);
                    break;
                case 5://设置
                    Obj_GameSettingBack.SetActive(true);
                    break;
                case 6://好友
                    Obj_FriendListBacK.SetActive(true);
                    break;
                case 7://好友
                    if (Uicustmos == null)
                    {
                        UI ui = this.GetParent<UI>();
                        Uicustmos = ui.GetComponent<UICustomsComponent>();
                        Uicustmos.OnHuoqu();

                    }

                    UigamePage.GetFuchou().Coroutine();
                    Fuchou_Panel.SetActive(true);
                    Fuchou_Panel.transform.localScale = new Vector3(1, 1, 1);
                    break;
                default:
                    break;
            }
        }
        private void OnExitfuchoubang()
        {
            Fuchou_Panel.SetActive(false);
        }
        #endregion

        #region//排行榜
        private void OnBtnhaoyouRanking()
        {
            //Btn_FriendsList.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_6");
            //Btn_WorldRankings.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_5");
            //Btn_LocalRankings.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_5");
        }

        private void OnBtn_hijieRanking()
        {
            //Btn_FriendsList.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_5");
            //Btn_WorldRankings.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_6");
            //Btn_LocalRankings.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_5");

        }

        //获取世界排行榜
        private G2C_GetRankList g2rshijierangklist;
        private async ETVoid GetRanklist()
        {
            try
            {
                Log.Debug("==============世界排行榜");
                G2C_GetRankList g2rrangklist = (G2C_GetRankList)await SessionComponent.Instance.Session.Call(new C2G_GetRankList());//发送消息收到回调
                if (g2rrangklist.Error != ErrorCode.ERR_Success) //不成功
                {
                    Log.Debug("==========================世界排行榜" + g2rrangklist.Message);
                    return;
                }

                g2rshijierangklist = null;
                g2rshijierangklist = g2rrangklist;
                ScrollViewEx sve = Obj_ScrollView.GetComponent<ScrollViewEx>();
                //for (int TheCurrentDigital = 0; TheCurrentDigital < Obj_ScrollView.transform.GetChild(0).transform.childCount; TheCurrentDigital++)
                //{
                //    Destroy(Obj_ScrollView.transform.GetChild(0).transform.GetChild(TheCurrentDigital).gameObject);
                //}
                sve.SetDataCount(g2rshijierangklist.Infos.count);
                sve.OnSetItem = ShijieRankItem;
                sve.ReSetScrollView();

            }
            catch (Exception e)
            {
                Log.Debug("==============列表");
                Log.Error(e);
            }
        }
        private void ShijieRankItem(Transform tra, int idex)
        {
            if (idex < 3)
            {
                tra.transform.Find("Img_paihang").GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_" + (idex + 1));
                tra.transform.Find("Img_paihang").GetComponent<Image>().SetNativeSize();
                tra.transform.Find("Img_paihang/Text").gameObject.SetActive(false);
            }
            else
            {
                tra.transform.Find("Img_paihang").GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_4");
                tra.transform.Find("Img_paihang").GetComponent<Image>().SetNativeSize();
                tra.transform.Find("Img_paihang/Text").GetComponent<Text>().text = (idex + 1).ToString();
                tra.transform.Find("Img_paihang/Text").gameObject.SetActive(true);
            }
            UigamePage.FuchouEnemy(tra.Find("touxiangkuang/Image/Img_RankingTouxiang").GetComponent<Image>(), g2rshijierangklist.Infos[idex].FaceUrl);
            tra.transform.Find("Txt_Rankingname").GetComponent<Text>().text = g2rshijierangklist.Infos[idex].NickName;
            tra.transform.Find("Image/Text").GetComponent<Text>().text = g2rshijierangklist.Infos[idex].Star.ToString();
            tra.gameObject.SetActive(true);
            tra.transform.localScale = new Vector3(1, 1, 1);
        }

        private void OnBtnbendiRanking()
        {
            Btn_FriendsList.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_5");
            Btn_WorldRankings.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_5");
            Btn_LocalRankings.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_6");
        }

        private void OnBtnExitRanking()
        {
            Obj_RankingList.SetActive(false);
            Obj_MoreOptions.SetActive(true);
        }
        #endregion

        #region//邀请召回
        private void OnBtnrecall()
        {
            Btn_InviteButton.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_6");
            Btn_RecallFriends.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_5");
            //  Facebooklogin.Instance.GetFBFriendInGame();
        }

        private void OnBtnInvitation()
        {
            Btn_InviteButton.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_5");
            Btn_RecallFriends.GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Ranking_6");
            // Facebooklogin.Instance.Fenxiang();
        }

        private void OnBtnExitInvitationRecall()
        {
            Obj_InviteFriend.SetActive(false);
            Obj_MoreOptions.SetActive(true);
        }
        #endregion

        #region//VIP
        private void OnBtnExitViptc()
        {
            Obj_VipTc.SetActive(false);
        }

        //购买周卡
        private void OnBtnVipWeek()
        {

        }


        //购买月卡
        private void OnBtnVIpMonth()
        {

        }


        #endregion

        #region//地图

        #endregion

        #region//消息
        private void OnBtnExitMessage()
        {
            Obj_GameNewsBack.SetActive(false);
        }

        private List<G2C_NoticeMessage> notiecelist = new List<G2C_NoticeMessage>();
        public void OnsetMessage(G2C_NoticeMessage notice)
        {
            Log.Debug("收到推送消息");
            Log.Debug("收到推送消息" + Obj_GameNewsBack.name);
            Obj_GameNewsBack.transform.localScale = new Vector3(1, 1, 1);
            notiecelist.Add(notice);
            ScrollViewEx sve = Obj_Scrollhuadong.GetComponent<ScrollViewEx>();
            sve.SetDataCount(notiecelist.Count);
            sve.OnSetItem = OnsetMessageItem;
            sve.ReSetScrollView();

        }

        private void OnsetMessageItem(Transform tra, int idex)
        {

            UigamePage.FuchouEnemy(tra.Find("touxiang/Image/Image").GetComponent<Image>(), notiecelist[idex].FaceUrl);
            tra.Find("touxiang/Image1/Text").GetComponent<Text>().text = notiecelist[idex].Star.ToString();
            tra.Find("name").GetComponent<Text>().text = notiecelist[idex].NickName;
            tra.Find("Text").GetComponent<Text>().text = notiecelist[idex].Context.ToString();
            Log.Debug("攻击类型" + notiecelist[idex].MsgType);
            switch (notiecelist[idex].MsgType)
            {
                case MsgEnum.Steal:
                    tra.Find("xingxing").GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Steal_0");
                    break;
                case MsgEnum.Attack:
                    Log.Debug("生成图片");
                    tra.Find("xingxing").GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Steal_1");
                    break;
                case MsgEnum.Defense:
                    tra.Find("xingxing").GetComponent<Image>().sprite = MoreOptionsResources.Get<Sprite>("Steal_2");
                    break;
                case MsgEnum.EnergyFull:

                    break;
                default:
                    break;
            }

            tra.localScale = new Vector3(1, 1, 1);
            tra.gameObject.SetActive(true);

        }
        #endregion

        #region//设置
        private bool SetyinxiaoBool = true;
        private bool SetyinyueoBool = true;
        private bool SetzhengdongBool = true;
        private bool SetliaotianBool;
        private bool SetVIPBool;

        private void OnBtnExiteSeting()
        {
            Obj_GameSettingBack.SetActive(false);
            Obj_MoreOptions.SetActive(true);
        }


        private void OnBtnsetzhengdong()
        {
            SetzhengdongBool = !SetzhengdongBool;
            Qiehuan(Btn_SetTheVibration.GetComponent<Image>(), SetzhengdongBool);
        }

        private void OnBtnsetyinyue()
        {
            SetyinyueoBool = !SetyinyueoBool;
            Qiehuan(Btn_SetUpTheMusic.GetComponent<Image>(), SetyinyueoBool);
        }

        private void OnBtnsetyinxiao()
        {
            SetyinxiaoBool = !SetyinxiaoBool;
            Qiehuan(Btn_SetUpTheSound.GetComponent<Image>(), SetyinxiaoBool);
        }

        //切换当前按钮的图片
        private void Qiehuan(Image spr, bool bo)
        {
            if (bo)
            {
                spr.sprite = MoreOptionsResources.Get<Sprite>("Setqueren");
            }
            else
            {
                spr.sprite = MoreOptionsResources.Get<Sprite>("Setforen");
            }

        }

        #endregion

        #region//好友
        private void OnBtnInviteFriends()
        {
            Facebooklogin.Instance.FBInvite("come");
        }

        private void OnBtnExitFriendl()
        {
            Obj_FriendListBacK.SetActive(false);
            Obj_MoreOptions.SetActive(true);
        }
        #endregion

        #region//修改个人信息
        private void OnBtnExitAmendinfo()
        {
            Obj_AmendinfoBack.SetActive(false);
        }

        #endregion
        #endregion

    }
}
